package com.hh.fudan.project_management.adapters;

import java.util.List;

import com.hh.fudan.project_management.R;
import com.hh.fudan.project_management.bean.ListItemResult;
import com.hh.fudan.project_management.bean.ViewCache;
import com.hh.fudan.project_management.loader.AsyncImageLoader;
import com.hh.fudan.project_management.loader.AsyncImageLoader.ImageCallback;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class ImageAndTextListAdapter extends ArrayAdapter<ListItemResult>{
	private ListView listView;  
    private AsyncImageLoader asyncImageLoader;  

    public ImageAndTextListAdapter(Activity activity, List<ListItemResult> imageAndTexts, ListView listView) {  
        super(activity, 0, imageAndTexts);  
        this.listView = listView;  
        asyncImageLoader = new AsyncImageLoader();  
    }  

    public View getView(int position, View convertView, ViewGroup parent) {  
        Activity activity = (Activity) getContext();  

        // Inflate the views from XML  
        View rowView = convertView;  
        ViewCache viewCache;  
        if (rowView == null) {  
            LayoutInflater inflater = activity.getLayoutInflater();  
            rowView = inflater.inflate(R.layout.list_item, null);  
            viewCache = new ViewCache(rowView);  
            rowView.setTag(viewCache);  
        } else {  
            viewCache = (ViewCache) rowView.getTag();  
        }  
        ListItemResult imageAndText = getItem(position);  

        // Load the image and set it on the ImageView  
        String imageUrl = imageAndText.getUrl();  
        ImageView imageView = viewCache.getImageView();  
        imageView.setTag(imageUrl);  
        Drawable cachedImage = asyncImageLoader.loadDrawable(imageUrl, new ImageCallback() {  
            public void imageLoaded(Drawable imageDrawable, String imageUrl) {  
                ImageView imageViewByTag = (ImageView) listView.findViewWithTag(imageUrl);  
                if (imageViewByTag != null) {  
                    imageViewByTag.setImageDrawable(imageDrawable);  
                }  
            }  
        });  
        if (cachedImage == null) {  
            imageView.setImageResource(R.drawable.icon);  
        }else{  
            imageView.setImageDrawable(cachedImage);  
        }  
        // Set the text on the TextView  
        List<TextView> textView = viewCache.getTextView();  

        textView.get(0).setText(imageAndText.getPrice());
        textView.get(1).setText(imageAndText.getDealValue());
        textView.get(2).setText(imageAndText.getTitle());
        textView.get(3).setText(imageAndText.getOid());

        return rowView;  
    }  
}
